#include <iostream>
using namespace std;

// 直接插入排序函数
void insertionSort(int arr[], int n) {
    for (int i = 1; i < n; ++i) {
        int key = arr[i];
        int j = i - 1;

        // 将arr[0..i-1]中大于key的元素向后移动一位
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

int main() {
    int n;
    cout << "请输入数组的大小: ";
    cin >> n;

    int* R = new int[n]; // 动态分配数组内存

    cout << "请输入 " << n << " 个整数: ";
    for (int i = 0; i < n; i++) {
        cin >> R[i];
    }

    cout << "排序前的数组: ";
    for (int i = 0; i < n; i++) {
        cout << R[i] << " ";
    }
    cout << endl;

    insertionSort(R, n);

    cout << "排序后的数组: ";
    for (int i = 0; i < n; i++) {
        cout << R[i] << " ";
    }
    cout << endl;

    delete[] R; // 释放动态分配的内存

    return 0;
}